/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// CHScanner.cpp : Defines the class behaviors for the application.

#include "stdafx.h"
#include "CHScanner.h"
#include ".\chscanner.h"

#include "Messages.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(CHScannerApp, CWinApp)
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

DlgMain* mainDialog;

//---------------------------------------------------------------------------

CHScannerApp::CHScannerApp()
{
	mainDialog = NULL;

	runMode = __rmNormal;
	exitCode = 0;
	exitRunningTime.Empty();
	exitExportedFile.Empty();

	memset(fullExeName, 0, (MAX_PATH+1) * sizeof(TCHAR));

	eventLog = NULL;
}

//---------------------------------------------------------------------------

CHScannerApp theApp;

//---------------------------------------------------------------------------

BOOL CHScannerApp::InitInstance()
{
	// InitCommonControls() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	InitCommonControls();

	CWinApp::InitInstance();

	GetModuleFileName(NULL, fullExeName, MAX_PATH);

	SetRegistryKey(_T("Calin Radoni"));

	ParseCommandLine(chscli);

	CString strPath;
	strPath = fileOp.GetPathFromExistingFileName(fullExeName);

	try{
		eventLog = new MEventLog(_T("CHScanner"));
	}
	catch(...){
		eventLog = NULL;
	}
	if(eventLog!=NULL)
		eventLog->RegisterThisAppAsSource(1);

	DlgMain dlg;
	m_pMainWnd = &dlg;
	dlg.applicationPath = strPath;
	dlg.eventLog = eventLog;

	dlg.autoRunParameter.Empty();
	switch(chscli.command){
		case __ccNormalMode:
			dlg.autoRunMode = __rmNormal;
			break;

		case __ccAutoMode :
			dlg.autoRunMode = __rmAutoScan;
			dlg.autoRunParameter = chscli.parameter;
			break;

		case __ccPassiveMode :
			dlg.autoRunMode = __rmPassive;
			dlg.autoRunParameter = chscli.parameter;
			break;

		default:
			dlg.autoRunMode = __rmError;
			break;
	}

	runMode = dlg.autoRunMode;

	if(dlg.autoRunMode==__rmError){
		// unknown supplied parameter, quit
		m_pMainWnd = NULL;
		exitCode = AUTO_END_INVALID_PARAM;
		return FALSE;
	}

	mainDialog = &dlg;

	INT_PTR nResponse = dlg.DoModal();

	mainDialog = NULL;

	switch(nResponse){
		case IDOK :
			break;
		case IDCANCEL :
			break;
	}

	exitCode         = dlg.exitCode;
	exitRunningTime  = dlg.exitRunningTime;
	exitExportedFile = dlg.exitExportedFile;

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

//---------------------------------------------------------------------------
void CHScannerApp::ReportExitEvent(void)
{
	if(runMode==__rmNormal) return;

	switch(exitCode){
		case AUTO_END_SUCCESS :
			eventLog->ReportAnEvent(EVENTLOG_SUCCESS,    CATEG_General, MSG_AUTO_END_SUCCESS, exitRunningTime, exitExportedFile, "");
			break;
		case AUTO_END_INVALID_PARAM :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_AUTO_END_INVALID_PARAM, "");
			break;
		case AUTO_END_INVALID_RUN_TIME :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_AUTO_END_INVALID_RUN_TIME, "");
			break;
		case AUTO_END_COULDNOTSETTIMER :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_AUTO_END_COULDNOTSETTIMER, "");
			break;
		case AUTO_END_COULDNOTLOADFILE :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_AUTO_END_COULDNOTLOADFILE, "");
			break;
		case END_INIT_FAILED :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_END_INIT_FAILED, "");
			break;
		case AUTO_END_FAILEDTOSCAN :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_AUTO_END_FAILEDTOSCAN, "");
			break;
		case END_EXPORT_FAILED :
			eventLog->ReportAnEvent(EVENTLOG_ERROR_TYPE, CATEG_General, MSG_END_EXPORT_FAILED, exitRunningTime, "");
			break;
	}
}

int CHScannerApp::ExitInstance()
{
	if(eventLog!=NULL){
		ReportExitEvent();
		delete eventLog;
		eventLog = NULL;
	}
	
	CWinApp::ExitInstance();

	return exitCode;
}

//---------------------------------------------------------------------------
